import bsddb
import sys
import struct

urlid = bsddb.btopen('db.url-id','r')
idurl = bsddb.btopen('db.id-url','r')
title = bsddb.btopen('db.title','r')

binfile = open('db.webgraph','rb')
webgraph = {}

intsize = struct.calcsize('i')
tmp = 0
while 1:
    data = binfile.read(intsize)
    if data == '':
        break
    src = str(struct.unpack('i',data)[0])

    data = binfile.read(intsize)
    if data == '':
        break
    outdegree = str(struct.unpack('i',data)[0])

    dest = []
    for i in range(int(outdegree)):
        data = binfile.read(intsize)
        if data == '':
            break
        dest.append(str(struct.unpack('i',data)[0]))
    webgraph[str(src)] = dest
binfile.close()

while True:
    for i in range(5):
        print urlid.next()
    site = raw_input('site: ')
    if urlid.has_key(site) :
        siteid = urlid[site]
        print '>> URL  : ',site
        if title.has_key(siteid) :
            t = title[siteid]
            print '>> Title: ',t
        print ''

        if webgraph.has_key(siteid) :
            dest = webgraph[siteid]
            for d in dest:
                url = idurl[d]
                print url
                if title.has_key(d) :
                    print title[d]
                print '----'
    else:
        print 'site not found'

idurl.close()
urlid.close()
title.close()
